﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Stimulsoft.Report;
using Portal.Web.Global;
using Portal.Web.Framework;
using Portal.Core;
using Portal.AssetTn.Model;


namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AssetTN_PrintAssetListOption : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AssetTN.PrintAssetListOption";
            }
        }


        public override WebPartServiceCls WebPartService
        {
            get
            {
                return AssetUtility.WebPartService;
            }
        }

        public override string Title
        {
            get
            {
                return "In ấn hiện vật";
            }
        }

        public override bool RunStandalone
        {
            get
            {
                return false;
            }
        }

        public class MrtFile
        {
            public string Id;
            public string Title;
            public MrtFile(string _Id, string _Title)
            {
                Id = _Id;
                Title = _Title;
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {
                string SiteId = loadParamCls.SiteId;
                Environments.CheckSessionTimeOut(true);
               

                string SessionId = (string)Environments.Request("SessionId");
                if (string.IsNullOrEmpty(SessionId)) throw new Exception("Tham số truy cập SessionId không hợp lệ!");
                string Subject = loadParamCls.RefTitle;
                if (string.IsNullOrEmpty(Subject))
                {
                    Subject = Title;
                }
                string MrtFile = (string)Environments.Request("MrtFile");
                MrtFile[] MrtFiles=new MrtFile[]
                {
                    new MrtFile("phieu-anh.mrt","Phiếu ảnh"),
                    new MrtFile("phieu-kiem-ke-khoa-hoc.mrt","Phiếu kiểm kê khoa học"),
                    new MrtFile("phich.mrt","Phích tra cứu"),
                };
                string SelectExportOption = "<select class=drplist type=select id=drpSelectOption style=\"width:100%;\">\r\n";
                for (int iIndex = 0; iIndex < MrtFiles.Length; iIndex++)
                {
                    if (MrtFiles[iIndex].Id.Equals(MrtFile))
                    {
                        SelectExportOption +=
                            "   <option selected value=\"" + MrtFiles[iIndex].Id + "\">" + MrtFiles[iIndex].Title + "</option>\r\n";
                    }
                    else
                    {
                        SelectExportOption +=
                            "   <option value=\"" + MrtFiles[iIndex].Id + "\">" + MrtFiles[iIndex].Title + "</option>\r\n";
                    }
                }
                SelectExportOption+="</select>\r\n";
                string RetHtml =
                        " <script language=javascript>\r\n" +
                        "   function ClientExport()\r\n" +
                        "   {\r\n" +
                        "       SiteId='" + SiteId + "';\r\n" +
                        "       SessionId =  '"+SessionId+"';\r\n"+
                        "       MrtFile =  document.getElementById('drpSelectOption').value;\r\n" +
                        "       AjaxReturn  =  Portal.Gui.Web.WebParts.AssetTn.AssetTN_PrintAssetListOption.ServerSidePrint(SiteId, SessionId, MrtFile).value;\r\n" +
                        "       if(AjaxReturn.Error)\r\n" +
                        "       {\r\n" +
                        "           alert(AjaxReturn.InfoMessage);\r\n" +
                        "           return;\r\n" +
                        "       }\r\n" +
                        "       window.open(AjaxReturn.RetUrl,'_self');\r\n" +
                        "   }\r\n" +
                        " </script>\r\n" +
                        " <div id=\"" + loadParamCls.Id + "\"> \r\n" +
                        " <input id=txtSessionId type=hidden value=\"" + SessionId + "\">\r\n" +
                        " <table class=\"il_Block\" datatable=\"0\">\r\n" +
                        " <tbody>\r\n";
                if (loadParamCls.VisibleTitle)
                {
                    RetHtml +=
                        " <tr>\r\n" +
                        "     <td class=\"ilBlockHeaderBig\" colspan=\"1\">\r\n" +
                        "       <div style=\"float:left;\">\r\n" +
                        "         <h3 class=\"ilBlockHeader\"><a class=\"ilAccAnchor\" name=\"" + loadParamCls.Id + "_blhead\" id=\"" + loadParamCls.Id + "_blhead\">" + Subject + "</a></h3> <span id=\"" + loadParamCls.Id + "_loader\"></span>\r\n" +
                        "       </div>\r\n" +
                        "     </td>\r\n" +
                        " </tr>\r\n";
                }

                RetHtml +=
                        "   <tr class=\"tblrow1\">\r\n" +
                        "       <td style=\"padding:4px;\">\r\n" +
                        "       <h1>In danh sách theo các loại biểu mẫu</h1>\r\n" +
                        "           <table>\r\n" +
                        "               <tr>\r\n" +
                        "                   <td style=\"width:150px;valign:top;\"><img style=\"width:150px;float:left;\" border=\"0\" src=\"./templates/default/images/print_large.png\"></td>\r\n" +
                        "                   <td style=\"valign:top;\">\r\n" +
                        "                       Xin vui lòng chọn loại biểu mẫu để in:" +SelectExportOption+
                        "                       <ul style=\"margin-left:10px;list-style-position:inside;list-style-type:square;\">\r\n" +
                        "                           <li>Hệ thống hỗ trợ các định dạng xuất dữ liệu ra các dạng như pdf hoặc excel</li>\r\n" +
                        "                           <li>Trong trường hợp cần in chính xác như bản gốc hãy chọn xuất ra pdf</li>\r\n" +
                        "                           <li>Xuất ra excel trong trường hợp muốn lấy dữ liệu đầu ra để chỉnh sửa phục vụ việc khác</li>\r\n" +
                        "                       </ul>\r\n" +
                        "                       <input type=button class=button value=\"Thực hiện xuất dữ liệu để in\" onclick=\"javascript:ClientExport();\"></td>\r\n" +
                        "                   </td>\r\n" +
                        "               </tr>\r\n" +
                        "           </table>\r\n" +
                        "       </td>\r\n" +
                        "   </tr>\r\n" +
                        "  </tbody>\r\n" +
                        "</table>\r\n" +
                        "</div>\r\n" +
                        "<div id=divProcessing></div>\r\n" +
                        "<div id=divSearchResult>\r\n" +
                        "</div>\r\n";

                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSidePrint(
            string SiteId,
            string SessionId,
            string MrtFile)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                //doan nay tim kiem
                Environments.CheckSessionTimeOut(true);
                oAjaxReturn.RetUrl = "Print.aspx?wpid=" + WebPartId + "&SessionId=" + SessionId + "&MrtFile=" + MrtFile;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

      

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetTN_PrintAssetListOption));
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetRenderPrintBll));
        }
    }
}
